/**
 * Created by 12452 on 2017/10/20.
 */
var receiver = '';
var mobile = '';
var orderno = '';
var orderno_suplier = '';
//时间戳转化
function getLocalTime(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
function ajax() {
    // $.ajax({
    //     url: 'http://192.168.0.127:8080/kaa/query?receiver='+receiver+'&mobile='+mobile+'&orderno='+orderno+'&orderno_suplier='+orderno_suplier+'',
    //     type: 'GET',
    //     success: function (res) {
    //         console.log(res)
    //         var tr = '';
    //         for(var i = 0;i < res.data.length; i++){
    //             if(res.data[i].list.status == 1){
    //                 res.data[i].list.status = '已发货';
    //             }else if(res.data[i].list.status == 0){
    //                 res.data[i].list.status = '待发货';
    //             }
    //             if(res.data[i].list.orderstatus_suplier == 0){
    //                 res.data[i].list.orderstatus_suplier = '未采购';
    //             }else if(res.data[i].list.orderstatus_suplier == 1){
    //                 res.data[i].list.orderstatus_suplier = '采购中';
    //             }else if(res.data[i].list.orderstatus_suplier == 2){
    //                 res.data[i].list.orderstatus_suplier = '已发货';
    //             }
    //             res.data[i].list.orderdate = getLocalTime(res.data[i].list.orderdate / 1000);
    //         }
    //         // $('#right-order-list').append(tr);
    //         for(var i = 0; i < $('.status').length; i++){
    //             if($('.status')[i].innerHTML == '已发货'){
    //                 $('.editDel')[i].style.display = 'none';
    //                 $('.delivery')[i].style.display = 'none';
    //                 $('.delivery_false')[i].style.display = 'block';
    //             }else if($('.status')[i].innerHTML == '待发货'){
    //                 $('.editDel')[i].style.display = 'block';
    //                 $('.delivery')[i].style.display = 'block';
    //                 $('.delivery_false')[i].style.display = 'none';
    //             }
    //         }
    //     },
    //     error: function (err) {
    //         console.log(err)
    //     }
    // })
    var res = {
        data:[
            {color_id: "1", del_flag: null,delivery_company: null, href: "http://192.168.0.127:8080/kaa/img/pic1.png",id: "1",name: "11",names:null,orderdate: 1508291748000,
        orderno: "2", orderstatus_suplier: "2", price: 2, qty: 2, size_id: "2", status: "1", subtotal: 2,list: [{color_id: "1", del_flag: null,delivery_company: null, href: "http://192.168.0.127:8080/kaa/img/pic1.png",id: "1",name: "11",names:null,orderdate: 1508291748000,
                orderno: "2", orderstatus_suplier: "2", price: 2, qty: 2, size_id: "2", status: "1", subtotal: 2},{color_id: "1", del_flag: null,delivery_company: null, href: "http://192.168.0.127:8080/kaa/img/pic1.png",id: "1",name: "11",names:null,orderdate: 1508291748000,
                orderno: "2", orderstatus_suplier: "2", price: 2, qty: 2, size_id: "2", status: "1", subtotal: 2}]}
        ]
    };
    console.log(res.data[0].list);
    var table1 = '';
    var table = '';
  for(var i = 0 ;i < res.data.length; i++){
      res.data[i].list[0].orderdate = getLocalTime(res.data[i].list[0].orderdate / 1000)
      table1 += '<tbody class="tbody"><tr class="sales-order-state"><td><span class="color-blue">订单号:</span>'+res.data[i].list[0].orderno+'</td>' +
          '<td><span class="color-blue">下单日期：</span>'+res.data[i].list[0].orderdate+'</td>' +
          '<td><span class="color-blue">交易状态：</span>'+res.data[i].list[0].status+'</td>' +
          '<td><span class="color-blue">合智造采购状态：</span>'+res.data[i].list[0].orderstatus_suplier+'</td>' +
          '<td></td></tr></tbody>';
      // for(var j = 0; j < res.data[i].list.length; j++){
      //     table = '<tr><td><div class="order-info-wrap">' +
      //         '<img class="order-info-img"  src="'+res.data[i].list[j].href+'" alt="">' +
      //         '<div class="sales-order-info"><div class="order-info-name">'+res.data[i].list[j].name+'</div>' +
      //         '<div class="order-info-desc">颜色：<span>'+res.data[i].list[j].color_id+'</span>尺寸：<span>'+res.data[i].list[j].size_id+'</span></div>' +
      //         '</div></div></td><td>￥'+res.data[i].list[j].price+'</td><td>X'+res.data[i].list[j].qty+'</td><td colspan="'+res.data[i].list.length - 1+'">￥'+res.data[i].list[j].price+'</td><td colspan="'+res.data[i].list.length - 1+'">' +
      //         '<div class="delivery" data-toggle="modal" data-target="#sendNow">直接发货</div>' +
      //         '<div class="">订单详情</div><div class="delivery_false">查看快递单号</div>' +
      //         '<div class="sales-order-operate editDel" style="margin-top: 6px"><span>编辑</span><span>取消</span></div>' +
      //         '</td></tr>';
      // }
      table = '<tr><td><div class="order-info-wrap">' +
          '<img class="order-info-img"  src="'+res.data[i].list[0].href+'" alt="">' +
          '<div class="sales-order-info"><div class="order-info-name">'+res.data[i].list[0].name+'</div>' +
          '<div class="order-info-desc">颜色：<span>'+res.data[i].list[0].color_id+'</span>尺寸：<span>'+res.data[i].list[0].size_id+'</span></div>' +
          '</div></div></td><td>￥'+res.data[i].list[0].price+'</td><td>X'+res.data[i].list[0].qty+'</td><td colspan="'+res.data[i].list.length - 1+'">￥'+res.data[i].list[0].price+'</td><td colspan="'+res.data[i].list.length - 1+'>' +
          '<div class="delivery" data-toggle="modal" data-target="#sendNow">直接发货</div>' +
          '<div class="">订单详情</div><div class="delivery_false">查看快递单号</div>' +
          '<div class="sales-order-operate editDel" style="margin-top: 6px"><span>编辑</span><span>取消</span></div>' +
          '</td></tr>';
      console.log(table)
  }
    $('#table').append(table1);
};
ajax();
//搜索
$('#orderSearch').click(function () {
    receiver = $('#orderBuyer') .val();
    mobile = $('#orderBuyerPhone') .val();
    orderno = $('#orderInputNo') .val();
    orderno_suplier = $('#orderBuyNo') .val();
    ajax();
});
//重置
$('#orderReset').click(function () {
    receiver = $('#orderBuyer') .val('');
    mobile = $('#orderBuyerPhone') .val('');
    orderno = $('#orderInputNo') .val('');
    orderno_suplier = $('#orderBuyNo') .val('');
    ajax();
});
function detail(orderno) {
    location.href = 'orderDetail.html?id='+orderno;
};
function edit(orderno) {
    location.href = 'orderDetailEdit.html?id='+orderno;
}
//直接发货
function modal(orderno) {
    $('#sendNow').modal('show');
    $.ajax({
        url: 'http://192.168.0.127:8080/kaa/selectDelivery_company?orderno=' + orderno +'',
        type: 'GET',
        success: function (res) {
           if(res.status == 200){
               var option = '';
               for(var i = 0; i < res.data.length; i++){
                    option += '<option value='+res.data[i].list.delivery_company+'>'+res.data[i].list.delivery_company+'</option>'
               }
               $('#kuaidi').append(option);
           }else{
               alert('网络异常，请稍后再试...')
           }
        }
    });
    $('#fahuo').click(function () {
        var data = {
            orderno: orderno,
            delivery_company: $('#kuaidi').val(),
            delivery_no: $('#delivery_no').val()
        };
        $.ajax({
            url: 'http://192.168.0.127:8080/kaa/deliverGoods',
            type: 'POST',
            data: data,
            dataType: 'json',
            success: function (res) {
                if(res.status == 200){
                    $('#sendNow').modal('hide');
                    ajax();
                }else{
                    alert('网络异常，请稍后再试...')
                }
            }
        });
    })
}
